<?php
/**
 * Prado Portal 
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: GetMXRR.php 289 2008-11-14 05:34:40Z steen.rabol@gmail.com $
 * @package Pradoportal.Common.GetMXRR
 *
 */

 /**
 * GetMXRR function file
 *
 * @author Philippe Gaultier <pgaultier@gmail.com>
 * @copyright Original code under LGPL by Moriyoshi Koizumi <koizumi@ave.sytes.net>
 * @version $Revision: 289 $  $Date: 2008-11-14 05:34:40 +0000 (Fri, 14 Nov 2008) $
 * @package Pradoportal.External
 */

/**
 * $_NAMESERVER[]
 * The array that contains IP addresses or domain names of name servers
 * used for DNS resolution.
 * If nothing is set before require()'ing this library, the values will
 * automatically prepared.
 */
if(isset($_NAMESERVERS)
	&& (gettype($_NAMESERVERS)!="array"
	|| count($_NAMESERVERS)==0))
	unset($_NAMESERVERS);

/**
 * rewrite this path to the same as the box's configuration
 * if you run scripts on *NIX platforms
 */
define( 'RESOLV_CONF_PATH', '/etc/resolv.conf' );

require_once (dirname(__FILE__).'/XDns.php');

if( !isset( $_NAMESERVERS ) ) {
	$_NAMESERVERS = array();
	if( strncmp( PHP_OS, "WIN", 3 ) == 0 ) {
		unset( $res );
		@exec( 'ipconfig /all', $res );
		$cnt = count( $res );
		for( $i = 0; $i < $cnt; ++$i ) {
			if( strpos( $res[$i], 'DNS Servers' ) !== false ) {
				$_NAMESERVERS[] = substr( $res[$i], strpos( $res[$i], ': ' ) + 2 );
				break;
			}
		}
		while( $i<$cnt-1 && strpos( $res[++$i], ':' ) === false ) {
			$_NAMESERVERS[] = trim( $res[$i] );
		}
	} elseif( file_exists( RESOLV_CONF_PATH ) ) {
		$lines = file( RESOLV_CONF_PATH );
		$cnt = count( $lines );
		for( $i = 0; $i < $cnt; ++$i ) {
			list( $dr, $val ) = split( '[ \t]', $lines[$i] );
			if( $dr == 'nameserver' ) {
				$_NAMESERVERS[] = rtrim( $val );
			}
		}
		unset( $lines );
	}
}

if(count($_NAMESERVERS))
	$__PHPRESOLVER_RS = new DNSResolver( $_NAMESERVERS[0] );
else
{
	unset($_NAMESERVERS);
	unset($__PHPRESOLVER_RS);
}


/**
 * GetMXRR replacement to work undew windows
 * @param string hostname
 * @param array mxhosts
 * @param array weight
 * @return boolean status
 */
function GetMXRR( $hostname, &$mxhosts, &$weight ) {
	global $__PHPRESOLVER_RS;
	if(!isset($__PHPRESOLVER_RS))
		return(false);
	$dnsname = & DNSName::newFromString( $hostname );
	$answer = & $__PHPRESOLVER_RS->sendQuery(
			new DNSQuery(
				new DNSRecord( $dnsname, DNS_RECORDTYPE_MX )
			)
		);
	if( $answer === false || $answer->rec_answer === false ) {
		return false;
	} else {
		$i = count( $answer->rec_answer );
		$mxhosts = $weight = array();
		while( --$i >= 0 ) {
			if( $answer->rec_answer[$i]->type == DNS_RECORDTYPE_MX ) {
				$rec = &$answer->rec_answer[$i]->specific_fields;
				$mxhosts[] = substr( $rec['exchange']->getCanonicalName(), 0, -1 );
				$weight[] = $rec['preference'];
			}
		}
		return true;
	}
}

?>
